home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / mpfeel.lha / MPFeel / Modules / heapv2.em < prev    next >
Lisp/Scheme  |  1992-10-06  |  22KB  |  713 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                           ;;
  3. ;;  EuLisp Module                     Copyright (C) University of Bath 1991  ;;
  4. ;;                                                                           ;;
  5. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  6.  
  7. (defmodule heapv2 
  8.   (futures
  9.    threads
  10.    semaphores
  11.    arith
  12.    lists
  13.    extras
  14.    vectors
  15.    list-operators
  16.    streams
  17.  ) ()
  18.  
  19.  
  20. (setq lista1 nil)
  21. (setq lista2 nil)
  22.  
  23. (setq seed 253)
  24. (setq seed2 867)
  25.  
  26. (defun >= (x y)
  27.   (not (< x y))
  28. )
  29.  
  30. (defun <= (x y)
  31.   (not (> x y))
  32. )
  33.  
  34. (defun random100 ()
  35.    (progn
  36.       (setq seed (modulo (+ (* seed 1213) 277) 149))
  37.       (modulo seed 100)
  38.    )
  39. )
  40.  
  41. (defun random30 ()
  42.    (progn
  43.       (setq seed2 (modulo (+ (* seed2 3247) 913) 97))
  44.       (+ (modulo seed2 13) 1)
  45.    )
  46. )
  47.  
  48.  
  49. (defun create_pet (n)
  50.    (create_pet_aux () n 0)
  51. )
  52.  
  53.  
  54. (defun cont (x y)
  55.   (if (equal x nil) 
  56.       nil
  57.    (if (or
  58.           (and (>= (car y) (caar x)) (< (car y) (+ (caar x) (cdar x))))
  59.           (and (>= (caar x) (car y)) (< (caar x) (+ (car y) (cdr y))))
  60.         )
  61.       t
  62.       (cont (cdr x) y)
  63.    )
  64.   )
  65. )
  66.  
  67.  
  68. (defun create_pet_aux (x n c)
  69.    (if (not (< c n)) 
  70.       x 
  71.     (prog (a b)
  72. loop1
  73.       (setq a (random100))
  74.       (setq b (random30))
  75.       (if (> (+ a b) 100) (go loop1) nil)
  76.       (if (cont x (cons a b)) (go loop1)
  77.                  (if (= 0 (modulo c 2))
  78.                             (progn
  79.                               (setq lista1 (append lista1 (list (cons a b))))
  80.                               (create_pet_aux (append x (list (cons a b))) n (+ c 1))
  81.                             )
  82.                             (progn
  83.                               (setq lista2 (append lista2 (list (cons a b))))
  84.                               (create_pet_aux (append x (list (cons a b))) n (+ c 1))
  85.                             )
  86.                  )
  87.       )
  88.     )
  89.   )
  90. )
  91.  
  92. (defun scheduler (n)
  93.          (create_pet n)
  94.          (print lista1)
  95.          (print lista2)
  96.      (progn (future (process)) (process2))
  97. )
  98.  
  99. (defun process ()
  100.      (setq item (car lista))
  101.      (setq lista (cdr lista))
  102.      (if (null lista) (setq fin t) (setq fin f))
  103.      (insblk (car item) (cdr item))
  104.      (if fin nil (process))
  105. )
  106.  
  107. (defun process2 ()
  108.     (setq item (car lista2))
  109.     (setq lista2 (cdr lista2))
  110.     (if (null lista2) (setq fin t) (setq fin f))
  111.     (insblk (car item) (cdr item))
  112.     (if fin nil (process2))
  113. )
  114.                 
  115.  
  116. ;;; Rutinas de test de las inserciones y supresiones en el arbol.
  117. ;;; Test(n) genera n inserciones aleatorias,haciendo una supresion aleatoria 
  118. ;;; cada 4 inserciones a partir de la segunda.
  119.  
  120. (defun test (n)
  121.   (test-aux (create_pet n) 0)
  122. )
  123.  
  124. (defun test-aux (x n)
  125.     (print "*************************************")
  126.     (print (car x))
  127.     (insblk (caar x) (cdar x))
  128.     (print tuple_root)
  129.     (if (= (modulo n 4) 2) 
  130.         (progn 
  131.           (print "####################################")
  132.       (setq z (random30))
  133.       (print z)
  134.           (getblk z)
  135.       (print tuple_root)
  136.         )
  137.         nil
  138.     )
  139.     (if (equal (cdr x) nil)
  140.         nil
  141.         (test-aux (cdr x) (+ n 1))
  142.     )
  143. )
  144.  
  145. ;;; Constant definition
  146.  
  147. (defconstant block_size 5)
  148. (defconstant b_lock 0)
  149. (defconstant b_left 1)
  150. (defconstant b_right 2)
  151. (defconstant b_addr 3)
  152. (defconstant b_len 4)
  153.  
  154. (defconstant l_child t)
  155. (defconstant r_child nil)
  156.  
  157. (defconstant heap_size 100)
  158. (defconstant heap_base_addr 0)
  159. (defconstant extra_big (+ 1 heap_size))
  160.  
  161.  
  162. ;;; Function definition
  163.  
  164. (defun free (node) 
  165.  (print "entro en free")
  166.  (if (not (= 1 (vector-ref node b_lock)))
  167.       (progn
  168.     (print "******************************************************")
  169.     (print "Trying to free a node that is not locked")
  170.     (print "******************************************************")
  171.       )
  172.     nil
  173.  )     
  174.  (vector-ref-updator node b_lock 0)
  175.  (print "salgo de free")
  176. )
  177.  
  178. (defun <= (x y) (not (> x y)))    ;;; Do these functions exist ???
  179. (defun >= (x y) (not (< x y)))
  180.  
  181. ;;; Nodes are marked when they are accesed by the functions left and right.
  182. ;;; They are not marked by the functions leftw and rightw (the process
  183. ;;; just waits for them to be free before operating on them).
  184.  
  185. (defun left  (x)
  186.  (prog (var)
  187.     (print "Entrando en left y la x vale : ")
  188.     (print x)
  189.     (if (not (= 1 (vector-ref x b_lock)))
  190.        (progn
  191.         (print "***********************************************")
  192.         (print "Trying to get the left child without locking the parent")
  193.         (print "***********************************************")
  194.        )
  195.     nil
  196.     )
  197.        (setq var (vector-ref x b_left))
  198.        (if (null var) (return nil) nil)
  199. lb     (cond ( (= (vector-ref var b_lock) 0) 
  200.                (vector-ref-updator var b_lock 1)
  201.                (return var)     
  202.              )
  203.              ( t (go lb))
  204.        )
  205.  )
  206. )
  207.  
  208. (defun leftw  (x)
  209.  (prog (var)
  210.        (setq var (vector-ref x b_left))
  211.        (if (null var) (return nil) nil)
  212. lb     (cond ( (= (vector-ref var b_lock) 0) 
  213.                (return var)     
  214.              )
  215.              ( t (go lb))
  216.        )
  217.  )
  218. )
  219.  
  220. (defun right  (x)
  221.  (prog (var)
  222.     (print "Entrando en right la x vale : ")
  223.     (print x)
  224.     (if (not (= 1 (vector-ref x b_lock)))
  225.        (progn
  226.         (print "***********************************************")
  227.         (print "Trying to get the right child without locking the parent")
  228.         (print "***********************************************")
  229.        )
  230.     nil
  231.     )
  232.        (setq var (vector-ref x b_right))
  233.        (if (null var) (return nil) nil)
  234. lb     (cond ( (= (vector-ref var b_lock) 0) 
  235.                (vector-ref-updator var b_lock 1)
  236.                (return var)     
  237.              )
  238.              ( t (go lb))
  239.        )
  240.  )
  241. )
  242.  
  243.  
  244. (defun rightw  (x)
  245.  (prog (var)
  246.        (setq var (vector-ref x b_right))
  247.        (if (null var) (return nil) nil)
  248. lb     (cond ( (= (vector-ref var b_lock) 0) 
  249.                (return var)     
  250.              )
  251.              ( t (go lb))
  252.        )
  253.  )
  254. )
  255.  
  256.  
  257. (defun addr  (x)    (vector-ref x b_addr  ))
  258. (defun len   (x)    (vector-ref x b_len   ))
  259.  
  260.  
  261. (defun leftkkk  (x y)  (vector-ref-updator x b_left  y))
  262. (defun rightkkk (x y)  (vector-ref-updator x b_right y))
  263. (defun addrkkk  (x y)  (vector-ref-updator x b_addr  y))
  264. (defun lenkkk   (x y)  (vector-ref-updator x b_len   y))
  265.  
  266. (defun to_the_left_of (a b) 
  267.     (< (addr a) (addr b)))
  268.  
  269. (defun coalesces (left right)
  270.     (= (+ (addr left) (len left)) (addr right)))
  271.  
  272. (defun ok4size (parent child) 
  273.     (>= (len parent) (len child)))
  274.  
  275. (defun add2len (old new)
  276.     (lenkkk old (+ (len old) (len new))))
  277.  
  278. (defun fixparent (p waslft new)         ; update either left or right of a node
  279.     (if waslft (leftkkk p new ) 
  280.            (rightkkk p new)
  281.     )
  282. )
  283.  
  284. ; pretend that root is arbitrarily large to get insert to coalesce correctly on
  285. ; first real node
  286.  
  287. (defun make_block (base length)
  288.     (let ((new (make-vector block_size nil)))
  289.           (vector-ref-updator new b_lock 0)
  290.           (addrkkk new base)
  291.           (lenkkk new length)
  292.           new
  293.     )
  294. )
  295.  
  296. (defun setup_tuple_heap ()
  297.     (setq tuple_root (make_block (+ heap_base_addr heap_size) extra_big))
  298.     (leftkkk tuple_root (make_block heap_base_addr heap_size))
  299.  
  300.     (setq sem (make-semaphore))
  301.     (initialize-semaphore sem)
  302.  
  303. ;;; Inicialitzar el semafor de l'arrel, posteriorment caldra fer servir un 
  304. ;;; semafor de veritat.
  305.  
  306. )
  307.  
  308. (setup_tuple_heap)    ; set up made when loading the module
  309.  
  310. (defun insblk (adr leng)
  311.     (setq v (make-vector 5 nil))
  312.     (vector-ref-updator v b_lock 1)        ;the block to be inserted is locked
  313.     (addrkkk v adr)
  314.     (lenkkk v leng)
  315.  
  316. ;;; Wait del semafor de l'arrel de l'arbre.De moment no es fa com cal.
  317.  
  318.     (open-semaphore sem)
  319.  
  320.     (insert tuple_root l_child (left tuple_root) v)
  321. )
  322.  
  323. (defun insertfromroot (new)
  324.    (rightkkk new nil)
  325.    (leftkkk new nil)
  326.  
  327. ;;; Wait del semafor de l'arrel de l'arbre.Solucio temporal.
  328.  
  329.     (open-semaphore sem)
  330.  
  331.     (insert tuple_root l_child (left tuple_root ) new )
  332. )
  333.  
  334. (defun getblk(size)
  335.  
  336. ;;; Wait del semafor de l'arrel de l'arbre.Encara no ben fet.
  337.  
  338.  (open-semaphore sem)
  339.  
  340.  (let ((l_son (left tuple_root)))
  341.   (if (null l_son)
  342.     (progn
  343.       (print "Sorry,no memory left")
  344.       
  345.     (close-semaphore sem)
  346. ;;; Fer el signal del semafor de l'arrel.
  347.  
  348.       nil
  349.     )
  350.     (progn
  351.           (cond ((> size (len l_son))
  352.                (print "No large enough block exists.")
  353.                (print " Max : ")
  354.                (print (len l_son)) 
  355.                (print " Request : ")
  356.                (print size)
  357.  
  358.                    (free l_son)
  359.     (close-semaphore sem)
  360.  
  361. ;;; Signal del semafor de l'arrel,treure in-use fill esquerre.
  362.  
  363.                nil
  364.         )
  365.             (t
  366.                (getblk1 size l_son tuple_root t)
  367.             )
  368.            )
  369.     )
  370.    )
  371.  )
  372. )
  373. (defun getblk1 (size ptr last waslft)
  374.                      ; get a block of size from a descendant of ptr if 
  375.              ; possible, or split ptr otherwise
  376.  
  377.     (let ((l (left ptr)) (r (right ptr)))
  378.       (cond ((and (not (null l))
  379.           (<= size (len l)))
  380.              (if (= 100 (addr last)) (close-semaphore sem) (free last)) 
  381.              (if (not (null r)) (free r) nil)
  382.              ;;;en aquest punt alliberar r i last
  383.          (getblk1 size l ptr t))               ; get from left hand child
  384.         ((and (not (null r))
  385.           (<= size (len r)))
  386.              ;;; en aquest punt alliberar last i l
  387.              (if (not (null l)) (free l) nil)
  388.              (if (= 100 (addr last)) (close-semaphore sem) (free last)) 
  389.          (getblk1 size r ptr nil))             ; get from right hand child
  390.         (t
  391.              (if (not (null l)) (free l) nil)
  392.              (if (not (null r)) (free r) nil)
  393.              ;;; en aquest punt alliberar l i r
  394.          (splitblk size ptr last waslft))
  395.     )
  396.       )
  397. )
  398.         
  399.  
  400.         
  401. (defun splitblk (size ptr last waslft) ; allocate a block of size from the end
  402.                        ; of ptr and make ptr smaller
  403.     (let* ((l (len ptr)) (over (- l size))
  404.        (new (make_block (+ (addr ptr) over) size)))
  405. ;;; atencio, new es  un node que es fabrica nomes per a retornar a l'usuari
  406. ;;; pero que mai no s'incorpora a l'arbre...No cal marcar-lo ni res.
  407.       (cond ((= 0 over)             ; asked for the whole block
  408.          (delfixup last waslft (left ptr) (right ptr))
  409. ;;;Aqui no cal alliberar res
  410.          ; should perhaps null out left and right in ptr
  411.          (leftkkk ptr 'void)
  412.          (rightkkk ptr 'void)
  413.              (free ptr) ;innecesari,ningu hauria de poder arribar a ptr mai mes
  414. ;;; Aqui alliberar ptr
  415.          new)
  416.         (t
  417.          (lenkkk ptr over)             ; make ptr smaller
  418.          (reheapify last waslft ptr)
  419.          new)
  420.     )
  421.     )
  422. )
  423.  
  424.  
  425. (defun reheapify (parent waslft ptr) 
  426.     ; ptr may be too small, but is ok for addressing
  427.     (print "Entro en reheapify")
  428.     (print parent)
  429.     (print ptr)
  430.     (let* ((a (left ptr)) (b (right ptr)) (plen (len ptr))
  431.        (abig (and (not (null a)) ; abig true if left child too big
  432.               (> (len a) plen)))
  433.        (bbig (and (not (null b)) ; bbig true if right child too big
  434.               (> (len b) plen))))
  435.       (cond ((not abig) 
  436.          (cond ((not bbig)
  437.                     (if (not (null a)) (free a) nil) 
  438.                     (if (not (null b)) (free b) nil) 
  439.                     (free ptr)
  440.             (if (= 100 (addr parent)) (close-semaphore sem) (free parent)) 
  441.              nil
  442.             ) ; ptr was actually ok
  443.         (t
  444.             ; right hand child is bigger, left isn't
  445.          (fixparent parent waslft b)     ; parent points to old right
  446.              (if (= 100 (addr parent)) (close-semaphore sem) (free parent))         
  447.          (leftkkk ptr a)               ; hang old left onto left of ptr
  448.          (if (not (null a)) (free a) nil)
  449.          (rightkkk ptr (leftw b))       ; and left of old right on right
  450.          (leftkkk b ptr)               ; and put ptr as left of old right
  451.          (reheapify b l_child ptr))))      ; now check that          
  452.         (t                                 ; left child is bigger than ptr
  453.          (cond ((not bbig)                 ; and right isn't
  454.             (fixparent parent waslft a) ; parent points to old left
  455.             (if (= 100 (addr parent)) (close-semaphore sem) (free parent))    
  456.             (rightkkk ptr b)       ; hang old right onto right of ptr
  457.             (if (not (null b)) (free b) nil)
  458.             (leftkkk ptr (rightw a))  ; and right of old left on left  
  459.             (rightkkk a ptr)       ; and put ptr as right of old left
  460.             (reheapify a r_child ptr))    ; now check that          
  461.            ; both a children are bigger, so must put correct one on top
  462.            ((> (len a) (len b))         ; left is bigger than right
  463.             (fixparent parent waslft a) ; see comments above
  464.             (if (= 100 (addr parent)) (close-semaphore sem) (free parent))
  465.             (rightkkk ptr b)
  466.             (if (not (null b)) (free b) nil)
  467.             (leftkkk ptr (rightw a))
  468.             (rightkkk a ptr)
  469.             (reheapify a r_child ptr))
  470.            (t
  471.             (fixparent parent waslft b)
  472.             (if (= 100 (addr parent)) (close-semaphore sem) (free parent))
  473.             (leftkkk ptr a)
  474.             (if (not (null a)) (free a) nil)
  475.             (rightkkk ptr (leftw b))
  476.             (leftkkk b ptr)
  477.             (reheapify b l_child ptr)
  478.            )
  479.          )
  480.         )
  481.      )
  482.       )
  483.  
  484.  
  485.              
  486.              
  487. (defun delfixup (parent waslft a b)
  488.     (print "entro en delfixup")
  489.     (print parent)
  490.     (print a)
  491.     (print b)
  492.     ; we've deleted a node, so we've got a dangling pointer and two orphans.
  493.     (cond ((null a)
  494.           ;;; no cal alliberar a doncs si es null llavors no hi ha in_use...   
  495.                                        ; no left child
  496.        (if (null b)
  497.             ;;; el mateix d'abans aplicat a b
  498.                (progn 
  499.             (fixparent parent waslft nil) ; no children so make into a leaf
  500.                 (if (= 100 (addr parent)) (close-semaphore sem) (free parent))
  501.                )
  502.                (progn
  503.             (fixparent parent waslft b)   ; attach old right child
  504.                 (free b)
  505.                 (if (= 100 (addr parent)) (close-semaphore sem) (free parent))
  506.                )
  507.            )
  508.           )  
  509.                ;;; alliberar parent i b si no era null
  510.                ;;; s'ha fet introduint progn per a sequenciar...
  511.       (t 
  512.        (if (null b)
  513.                ;;; no alliberar b,  doncs era null
  514.                (progn
  515.             (fixparent parent waslft a) ; no right child
  516.                 (if (= 100 (addr parent)) (close-semaphore sem) (free parent)) 
  517.             (free a)
  518.                )
  519.                ;;; alliberar parent i a ; tambe fet amb progn
  520.             ; hard case, there are two children, so do a rotate
  521.         (cond ((> (len a) (len b))    ; old left is bigger, so
  522.            (fixparent parent waslft a)   ; dangling now to old left
  523.                 (if (= 100 (addr parent)) (close-semaphore sem) (free parent)) 
  524.                    ;;; alliberar parent 
  525.            (delfixup a r_child (right a) b)) ; fixup right of old left 
  526.                                          ; wrt old right of old left
  527.                                          ; and old right
  528.  
  529.           (t                             ; old right is bigger, so
  530.            (fixparent parent waslft b)   ; dangling now to old right
  531.                 (if (= 100 (addr parent)) (close-semaphore sem) (free parent)) 
  532.            ;;; alliberar parent
  533.            (delfixup b l_child a (left b)))))))
  534.      (print "salgo de delfixup")
  535. )
  536.  
  537.              
  538. (defun delfixupnm (parent waslft a b)
  539.     (print "entro en delfixupnm")
  540.     (print parent)
  541.     (print a)
  542.     (print b)
  543.     ; we've deleted a node, so we've got a dangling pointer and two orphans.
  544.     (cond ((null a)
  545.           ;;; no cal alliberar a doncs si es null llavors no hi ha in_use...   
  546.                                        ; no left child
  547.        (if (null b)
  548.             ;;; el mateix d'abans aplicat a b
  549.                (progn 
  550.             (fixparent parent waslft nil) ; no children so make into a leaf
  551.                )
  552.                (progn
  553.             (fixparent parent waslft b)   ; attach old right child
  554.                )
  555.            )
  556.           )  
  557.                ;;; alliberar parent i b si no era null
  558.                ;;; s'ha fet introduint progn per a sequenciar...
  559.       (t 
  560.        (if (null b)
  561.                ;;; no alliberar b,  doncs era null
  562.                (progn
  563.             (fixparent parent waslft a) ; no right child
  564.                )
  565.                ;;; alliberar parent i a ; tambe fet amb progn
  566.             ; hard case, there are two children, so do a rotate
  567.         (cond ((> (len a) (len b))    ; old left is bigger, so
  568.            (fixparent parent waslft a)   ; dangling now to old left
  569.                    ;;; alliberar parent 
  570.            (delfixupnm a r_child (rightw a) b)) ; fixup right of old left 
  571.                                          ; wrt old right of old left
  572.                                          ; and old right
  573.  
  574.           (t                             ; old right is bigger, so
  575.            (fixparent parent waslft b)   ; dangling now to old right
  576.            ;;; alliberar parent
  577.            (delfixupnm b l_child a (leftw b)))))))
  578.      (print "salgo de delfixupnm")
  579. )
  580.  
  581.             ; fixup left of old right wrt old left and old left of old right
  582.  
  583. ; insert is the hardest of all. When inserting a block it may coalesce with 
  584. ; 0, 1 or 2 existing blocks. If we have just performed a coalescence then the
  585. ; other coalescing block (if it exists) is in one of the children; found by
  586. ; leftc or rightc.
  587.  
  588. (defun leftc (parent waslft node end_addr) ; find a block ending at end_addr 
  589.     ;starting from node. If such a block exists it is the rightmost descendant,
  590.     ;and its left child (if any) can be spliced in in its place.
  591.  
  592.     (cond ((null node) nil)
  593.       ((= (+ (addr node) (len node)) end_addr)    ; it does coalesce
  594.       (fixparent parent waslft (leftw node)) ;delete node and reconnect left
  595.        node)
  596.       (t
  597.        (leftc node r_child (rightw node) end_addr)
  598.           )
  599.   )
  600. )
  601.  
  602. (defun rightc (parent waslft node start_addr) ;find a block starting at 
  603.                           ;start_addr from node, going left
  604.     (cond ((null node) nil)
  605.       ((= (addr node) start_addr)          ; it does coalesce
  606.        (fixparent parent waslft (rightw node)) ;delete node and reconnect right
  607.        node)
  608.       (t
  609.        (rightc node l_child (leftw node) start_addr)
  610.           )
  611.     )
  612. )
  613.  
  614. ; partition takes a tree (node), and a pivot element. It returns a tree, the
  615. ; root of which is pivot (with any coalescing blocks added to it), and whose
  616. ; children are correct wrt the root.
  617.  
  618. (defun partition (node pivot)
  619.     ; partition returns a node whose left and right children are correct
  620.     ; the node is the (modified) pivot
  621.     (print "entro en partition")
  622.     (print node)
  623.     (cond ((null node) 
  624.        (leftkkk pivot nil)
  625.        (rightkkk pivot nil)
  626.        pivot)
  627.       ((to_the_left_of node pivot)
  628.        (cond ((coalesces node pivot) ; pivot joins onto right end of node
  629.           (add2len node pivot)   ; merge node into pivot
  630.           (let ((rc (rightc node r_child (rightw node) 
  631.                     (+ (addr node) (len node)))))
  632.             ; rc modifies right branch in place
  633.             (cond ((not (null rc)) ;rc goes on right of new
  634.                (add2len node rc))))
  635.           node) ; node now has correct left and right children
  636.          (t                     ; node clear to left of pivot
  637.           ; thus the left children of node are ok
  638.           (let ((part (partition (rightw node) pivot)))
  639.          ; now transfer the left child of part to be right child of node
  640.             (rightkkk node (leftw part))
  641.             (leftkkk part node) ; and make node the left child of part
  642.             part)))) 
  643.       (t
  644.        (cond ((coalesces pivot node)  ; new joins to left of node
  645.           (addrkkk node (addr pivot)) ; node now begins at new
  646.           (add2len node pivot)            ; merge new into node
  647.           (let ((lc (leftc node l_child (leftw node) 
  648.                    (addr node))))
  649.             (cond ((not (null lc)) ; lc goes on left of node
  650.                (addrkkk node (addr lc))
  651.                (add2len node lc))))
  652.           node)
  653.          (t                     ; node clear to right of pivot
  654.           ; thus the right children of node are ok
  655.           (let ((part (partition (leftw node) pivot)))
  656.            ; now transfer the right child of part to be left child of node
  657.             (leftkkk node (rightw part))
  658.             (rightkkk part node) ; and make node the right child of part
  659.             part)))))
  660. )
  661.  
  662. (defun insert (parent waslft node new)
  663.     (cond ((null node)  ; make new into a leaf
  664.        (fixparent parent waslft new)
  665. ;;; alliberem parent,no cal alliberar node perque es nul
  666.            (if (= 100 (addr parent)) (close-semaphore sem) (free parent)) 
  667.        (free new)    ;;; bloc a insertar,esta marcat
  668.       )
  669.       ((and (not (coalesces node new)) ; if it coalesces we call partition
  670.         (not (coalesces new node)) ; lazy, but the loss isn't much
  671.         (> (len node) (len new))) ; we aren't big enough
  672.            ;;;aqui es pot alliberar parent, abans de fer el cond
  673.            (if (= 100 (addr parent)) (close-semaphore sem) (free parent)) 
  674.        (cond ((< (addr new) (addr node)) ; node goes on left of new
  675.           (insert node l_child (left node) new))
  676.          (t   ; new on the left of node
  677.           (insert node r_child (right node) new))))
  678.       (t 
  679. ; could be a coalescence
  680. ; new is now not smaller than node, so put it in place of node,
  681. ; partition the appropriate descendent, and fix up.
  682. ; we insert as soon as we can so thatif a coalescence occurs
  683. ; there's a chance we still fit.
  684.            (free new)
  685.            (free node)
  686.        (let ((p (partition node new)))
  687.          (cond ((ok4size parent p)
  688.             (fixparent parent waslft p)
  689. ;;; alliberar parent
  690.            (if (= 100 (addr parent)) (close-semaphore sem) (free parent)) 
  691.            ) ;we fit here,so fixup and leave
  692.            (t
  693.             (delfixupnm parent waslft (left p) (right p)) ; delete us
  694.            (if (= 100 (addr parent)) (close-semaphore sem) (free parent)) 
  695.                     (vector-ref-updator p b_lock 1)
  696.                     (print " >>>>>>>>>>>>>>>>> reinserto <<<<<<<<<<<<<<<")
  697.                     (insertfromroot p)            ; and start again
  698.            )
  699.          )
  700.        )
  701.       )
  702.     )
  703. )
  704.  
  705. ; note that the reinsertion from root cannot cause a coalescence, and thus 
  706. ; simplified code could be used. fix it later maybe.
  707.  
  708.  
  709. (export insblk getblk test scheduler)
  710.  
  711. )
  712.